/*******************************************************************************
 * Copyright (C) 2012, Google Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package com.google.eclipse.mechanic;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

import org.eclipse.core.runtime.ILog;

import com.google.eclipse.mechanic.plugin.core.IMechanicPreferences;
import com.google.eclipse.mechanic.plugin.core.MechanicLog;
import com.google.eclipse.mechanic.tests.internal.RunAsJUnitTest;

/**
 * Tests for {@link LastModifiedPreferencesFileTask}.
 * 
 * @History
 * 1. Add test for key/value pairs.
 * 1) Add a new field to TestTask: Map<String, String> keyPairs;
 * 2) Add a new method to TestTask: public Map<String, String> getKeyPairs();
 * 3) Add a new method to LastModifiedPreferencesFileTaskTest:
 *    Map<String, String> createNewMap().
 * @since Nov 20, 2014
 * @author jingjiejiang
 *
 */
@RunAsJUnitTest
public class LastModifiedPreferencesFileTaskTest extends TestCase {

  private IResourceTaskReference ref;
  private ILog ilog;
  private IMechanicPreferences prefs;
  private MechanicLog log;


  private static class TestTask extends LastModifiedPreferencesFileTask {
    private final String title;
    private final String description;
    
    // Add by Jingjie Jiang, Nov 20, 2014
    private final Map<String, String> keyPairs;

    public TestTask(
        IResourceTaskReference taskRef,
        IMechanicPreferences prefs,
        MechanicLog log,
        String title,
        String description,
        Map<String, String> keypairs) {
      super(taskRef, prefs, log);
      this.title = title;
      this.description = description;
      // Add by Jingjie Jiang, Nov 20, 2014
      this.keyPairs = keypairs;
    }

    public String getTitle() {
      return title;
    }

    public String getDescription() {
      return description;
    }

    // Add by Jingjie Jiang, Nov 20, 2014
    public Map<String, String> getKeyPairs() {
      return keyPairs;
    }
  }

  
  @Override
  protected void setUp() throws Exception {
    ref = mock(IResourceTaskReference.class);
    ilog = mock(ILog.class);
    prefs = mock(IMechanicPreferences.class);
    log = new MechanicLog(ilog);
    super.setUp();
  }

  @Override
  protected void tearDown() throws Exception {
//    verify(ref);
//    verify(ilog);
//    verify(prefs);
    super.tearDown();
  }

  public void testGetId() {
    when(ref.getPath()).thenReturn("/path/to");
    
    // Changed by Jingjie Jiang, Nov 20, 2014
    LastModifiedPreferencesFileTask task = new TestTask(ref, prefs, log, "X", "Y", createNewMap());

    assertEquals(
        "com.google.eclipse.mechanic.LastModifiedPreferencesFileTaskTest$TestTask@/path/to",
        task.getId());
  }

  public void testMd5_DoesNotExist() {
    when(ref.getPath()).thenReturn("/path/to");

    String key =
        "com.google.eclipse.mechanic.LastModifiedPreferencesFileTaskTest$TestTask@/path/to_lastmd5";

    when(prefs.contains(key)).thenReturn(false);

    // Changed by Jingjie Jiang, Nov 20, 2014
    LastModifiedPreferencesFileTask task = new TestTask(ref, prefs, log, "X", "Y", createNewMap());

    assertFalse(task.evaluate());

    verify(prefs).contains(key);
  }

  public void testMd5_ExistsButDoesNotMatch() throws Exception {
    when(ref.getPath()).thenReturn("/path/to");

    String key =
        "com.google.eclipse.mechanic.LastModifiedPreferencesFileTaskTest$TestTask@/path/to_lastmd5";

    when(prefs.contains(key)).thenReturn(true);
    when(prefs.getLong(key)).thenReturn(12345L);
    when(ref.computeMD5()).thenReturn(23456L);

    // Changed by Jingjie Jiang, Nov 20, 2014
    LastModifiedPreferencesFileTask task = new TestTask(ref, prefs, log, "X", "Y", createNewMap());

    assertFalse(task.evaluate());

    verify(prefs).contains(key);
    verify(prefs).getLong(key);
    verify(ref).computeMD5();
  }

  public void testMd5_ExistsButMatches() throws Exception {
    when(ref.getPath()).thenReturn("/path/to");

    String key =
        "com.google.eclipse.mechanic.LastModifiedPreferencesFileTaskTest$TestTask@/path/to_lastmd5";

    when(prefs.contains(key)).thenReturn(true);
    when(prefs.getLong(key)).thenReturn(12345L);
    when(ref.computeMD5()).thenReturn(12345L);

    // Changed by Jingjie Jiang, Nov 20, 2014
    LastModifiedPreferencesFileTask task = new TestTask(ref, prefs, log, "X", "Y", createNewMap());

    assertTrue(task.evaluate());

    verify(prefs).contains(key);
    verify(prefs).getLong(key);
    verify(ref).computeMD5();
  }
  
  //Add by Jingjie Jiang, Nov 20, 2014
  public Map<String, String> createNewMap() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("ABC", "123");
    
    return map;
  }
}
